package com.tianhui.consignortwofei.mvp.ui.activity.audit.driver;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.widget.itemView.ClickItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.DriverInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.authLicense.IdCardInfo;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.IdCardBackStrategy;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class BackAuditActivity extends BaseDriverAuditActivity {

    @BindView(R.id.activity_back_audit_objectClickItemView)
    ClickItemView mObjectClickItemView;
    @BindView(R.id.activity_back_audit_dateClickItemView)
    ClickItemView mDateClickItemView;

    private TimePickerView mEndDatePickerView;
    private String mBackImageId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_back_audit;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public void showDriverInfo(DriverInfo info) {
        mDateClickItemView.setContent(info.idcardendtime);
        loadPicture(info.imgidcardfphoto);
        mBackImageId = info.idcardfphoto;
    }

    @Override
    public DriverInfo getSaveInfo() {
        String date = mDateClickItemView.getContent();
        if (TextUtils.isEmpty(date)) {
            showToast("请选择身份证有效期");
            return null;
        }
        mDriverInfo.idcardendtime = date;
        mDriverInfo.idcardfphoto = mBackImageId;
        return mDriverInfo;
    }

    @Override
    public void nextAudit() {
        DriverInfo info = getSaveInfo();
        if (info != null) {
            Bundle bundle = new Bundle();
            bundle.putParcelable(KeyValue.KEY_DRIVER_INFO, info);
            bundle.putInt(KeyValue.KEY_OPERATE, getIntent().getExtras().getInt(KeyValue.KEY_OPERATE));
            openActivityForResult(DriverLicenseAuditActivity.class, bundle);
        }
    }

    //有效期至
    @OnClick(R.id.activity_back_audit_dateClickItemView)
    public void selectEndDate() {
        hideKeyboard();
        if (mEndDatePickerView == null) {
            mEndDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mEndDatePickerView.show();
    }

    //有效期至
    @OnClick(R.id.activity_back_audit_longDateButton)
    public void setLongDate() {
        mDateClickItemView.setContent("2099-12-31");
    }

    @Override
    public void changePictureSuccess(PictureInfo pictureInfo) {
        mBackImageId = pictureInfo.id;
    }

    @OnClick(R.id.layout_audit_picture_editImageView)
    public void changePicture() {
        IdCardBackStrategy idCardBackStrategy = (IdCardBackStrategy) pictureStrategyFactory.createStrategy(IdCardBackStrategy.class);
        pictureContext = new PictureContext(idCardBackStrategy);
        pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<IdCardInfo>() {
            @Override
            public void selectPhotoSuccess(SelectPhotoInfo<IdCardInfo> resultInfo) {
                mBackImageId = resultInfo.pictureId;
                loadPicture(resultInfo.realFileName);
                processIdCardBackResult(resultInfo.resultInfo);
            }

            @Override
            public void selectPhotoFailed() {

            }
        });
    }

    private void processIdCardBackResult(IdCardInfo resultInfo) {
        if (resultInfo == null) return;
        mDateClickItemView.setContent(resultInfo.idcardendtime);
    }
}
